package com.his.util.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.his.util.entity.RoleInterface;
import com.his.util.entity.SelectAllInterfaceRoleDo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author fall
 * @since 2022-10-22 06:44:14
 */
@Mapper
public interface RoleInterfaceMapper extends BaseMapper<RoleInterface> {
    /**
     * 查询用户是否可用该接口
     *
     * @param roleId
     * @param url
     * @return
     */
    @Select("select i.interface_id " +
            "        from t_interface as i " +
            "                 join t_role_interface as ri " +
            "                      on ri.role_id = #{roleId} and i.url = #{url} and i.interface_id = ri.interface_id " +
            "where i.is_open = 0 and i.is_delete = 0")
    Integer selectInterfaceIdByRoleIdAndUrl(Integer roleId, String url);

    /**
     * 查看角色菜单列表
     *
     * @param roleId
     * @param name
     * @param page
     * @param pageSize
     * @return
     */
    @Select("SELECT ri.role_interface_id as roleInterfaceId," +
            "       i.`name`             as `name`," +
            "       i.url                as url," +
            "       ri.role_id           as roleId," +
            "       i.interface_id       as interfaceId " +
            "   FROM t_interface AS i " +
            "       LEFT JOIN t_role_interface AS ri ON ri.role_id = #{roleId} " +
            "           AND i.interface_id = ri.interface_id " +
            "       WHERE i.`name` LIKE CONCAT(\"%\", #{name}, \"%\") " +
            "           AND i.is_delete = 0 " +
            "           AND i.is_open = 0 " +
            "           LIMIT #{page} " +
            "           , #{pageSize}")
    List<SelectAllInterfaceRoleDo> selectAll(int roleId, String name, int page, int pageSize);

    /**
     * 查看角色菜单列表个数
     *
     * @param roleId
     * @param name
     * @return
     */
    @Select("SELECT COUNT(i.interface_id) " +
            "   FROM t_interface AS i " +
            "       LEFT JOIN t_role_interface AS ri ON ri.role_id = #{roleId} " +
            "           AND i.interface_id = ri.interface_id " +
            "       WHERE i.`name` LIKE CONCAT(\"%\", #{name}, \"%\") " +
            "           AND i.is_delete = 0 " +
            "           AND i.is_open = 0 ")
    Long selectAllCount(int roleId, String name);
}
